package org.jhulk.input;

import org.jhulk.states.GameCache;
import org.jhulk.utils.GameUtils;
import org.jhulk.utils.TileMapUtils;
import org.newdawn.slick.Input;

public class KeyboardInputPlayControl 
{

	public GameCache keyPressed(int key, char c, GameCache cache)
	{
		if(cache.getSelectedChar() != null)
		{
			if(key == Input.KEY_LCONTROL || key == Input.KEY_RCONTROL) cache.setCtrlPressed( true );
			
	        if(cache.isCtrlPressed())
	        {
	            if(key == Input.KEY_LEFT) cache.getSelectedChar().turnLeft();
	
	            if(key == Input.KEY_RIGHT) cache.getSelectedChar().turnRight();
	        }
	        else
	        {
	            if(key ==  Input.KEY_LEFT && canMoveX(cache.getSelectedChar().getX(), cache.getSelectedChar().getY(), -1, cache))
	            {
	                if(key == Input.KEY_LEFT) cache.getSelectedChar().setX(cache.getSelectedChar().getX() - cache.getSelectedChar().getMoveX()); 
	            }
	
	            if(key == Input.KEY_RIGHT && canMoveX(cache.getSelectedChar().getX(), cache.getSelectedChar().getY(), 1, cache))
	            {
	                if(key == Input.KEY_RIGHT) cache.getSelectedChar().setX(cache.getSelectedChar().getX() + cache.getSelectedChar().getMoveX()); 
	            }
	        }
	
	        if(key == Input.KEY_UP && canMoveY(cache.getSelectedChar().getX(), cache.getSelectedChar().getY(), -1, cache))
	        {
	            if(key == Input.KEY_UP)cache.getSelectedChar().setY(cache.getSelectedChar().getY() - cache.getSelectedChar().getMoveY()); 
	        }
	
	        if(key == Input.KEY_DOWN && canMoveY(cache.getSelectedChar().getX(), cache.getSelectedChar().getY(), 1, cache))
	        {
	            if(key == Input.KEY_DOWN)cache.getSelectedChar().setY(cache.getSelectedChar().getY() + cache.getSelectedChar().getMoveY()); 
	        }
		}

        //if(key == Input.KEY_SPACE) performCharAction();

        if(key == Input.KEY_ESCAPE) System.exit(0);
		
		return cache;
	}
	
    public boolean canMoveX(int x, int y, int dir, GameCache cache)
    {
        boolean canMove = false;

        do
        {
            int futureXPos = (dir > 0) ? 
            		(cache.getSelectedChar().getX() + cache.getSelectedChar().getMoveX()) 
            		: 
            		(cache.getSelectedChar().getX() - cache.getSelectedChar().getMoveX());

            System.out.println("Current X pos: " + cache.getSelectedChar().getX());
            System.out.println("-future X position would be: " + futureXPos);

            //Check if there is a valid tileID
            int futureTileId = TileMapUtils.determineTileIdFromXY(futureXPos, cache.getSelectedChar().getY(), cache.getMap());

            if(futureTileId == 0)
            {
                System.out.println("Can't move there, no tile!");
                break;
            }

            //Check if the player can move to the tile or if something is blocking him
            String impassable = cache.getMap().getTileProperty(futureTileId, "impassable", "");
            if(impassable != "")
            {
                System.out.println("There is an impassable item in the way!!");
                break;
            }
            
            //Check if there is another character or item blocking us
            if(GameUtils.detectCollision(futureXPos, cache.getSelectedChar().getY(), cache))
            {
        		System.out.println("There is another character in the way!");
        		break;
            }
            
            canMove = true;
            break;

        } while(true);

        return canMove;
    }
    
    /**
     * Check the vertical bounds
     * @param x
     * @param y
     * @return
     */
    public boolean canMoveY(int x, int y, int dir, GameCache cache)
    {
        boolean canMove = false;
        do
        {
            int futureYPos = (dir > 0) ? 
            		(cache.getSelectedChar().getY() + cache.getSelectedChar().getMoveY()) 
            		: 
            		(cache.getSelectedChar().getY() - cache.getSelectedChar().getMoveY());

            System.out.println("Current Y pos: " + cache.getSelectedChar().getY());
            System.out.println("-future Y position would be: " + futureYPos);

            //Check if there is a valid tileID
            int futureTileId = TileMapUtils.determineTileIdFromXY(
            		cache.getSelectedChar().getX(), 
            		futureYPos, 
            		cache.getMap());

            if(futureTileId == 0)
            {
                System.out.println("Can't move there, no tile!");
                break;
            }

            //Check if the player can move to the tile or if something is blocking him
            String impassable = cache.getMap().getTileProperty(futureTileId, "impassable", "");
            
            if(impassable != "")
            {
                System.out.println("There is an impassable item in the way!!");
                break;
            }
            
            //Check if there is another character or item blocking us
            if(GameUtils.detectCollision(cache.getSelectedChar().getX(), futureYPos, cache))
            {
        		System.out.println("There is another character in the way!");
        		break;
            }
            

            canMove = true;
            break;

        } while(true);

        return canMove;
    }
	
}
